package com.aiiter.house.service.impl;

import java.util.*;

import com.aiiter.house.domain.HouseContract;
import com.aiiter.house.domain.HouseProperty;
import com.aiiter.house.domain.HouseRoomInformation;
import com.aiiter.house.domain.vo.HouseTenantInformationVo;
import com.aiiter.house.mapper.HouseContractMapper;
import com.aiiter.house.mapper.HousePropertyMapper;
import com.aiiter.house.mapper.HouseRoomInformationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aiiter.house.mapper.HouseTenantInformationMapper;
import com.aiiter.house.domain.HouseTenantInformation;
import com.aiiter.house.service.IHouseTenantInformationService;

import javax.annotation.Resource;

/**
 * 存储租户的基本信息及与房间的关联Service业务层处理
 *
 * @author yaocheng
 * @date 2024-08-13
 */
@Service
public class HouseTenantInformationServiceImpl implements IHouseTenantInformationService {
    @Resource
    private HouseTenantInformationMapper houseTenantInformationMapper;
    @Resource
    HousePropertyMapper housePropertyMapper;
    @Resource
    HouseRoomInformationMapper houseRoomInformationMapper;
    @Resource
    HouseContractMapper contractMapper;

    /**
     * 查询存储租户的基本信息及与房间的关联
     *
     * @param tenantId 存储租户的基本信息及与房间的关联主键
     * @return 存储租户的基本信息及与房间的关联
     */
    @Override
    public HouseTenantInformation selectHouseTenantInformationByTenantId(Long tenantId) {
        return houseTenantInformationMapper.selectHouseTenantInformationByTenantId(tenantId);
    }

    /**
     * 查询存储租户的基本信息及与房间的关联列表
     *
     * @param houseTenantInformation 存储租户的基本信息及与房间的关联
     * @return 存储租户的基本信息及与房间的关联
     */
    @Override
    public List<HouseTenantInformation> selectHouseTenantInformationList(HouseTenantInformation houseTenantInformation) {

        List<HouseTenantInformation> houseTenantInformations = houseTenantInformationMapper.selectHouseTenantInformationList(houseTenantInformation);
        for (HouseTenantInformation tenantInformation : houseTenantInformations) {
            String roomIdStr = tenantInformation.getRoomId();
            long[] roomIds;
            if (roomIdStr != null && !roomIdStr.isEmpty()) {
                // 去除字符串两端的方括号
                String trimmed = roomIdStr.substring(1, roomIdStr.length() - 1);
                // 使用逗号分隔字符串
                String[] roomIdArray = trimmed.split(",");
                // 将字符串数组转换为整数数组
                roomIds = Arrays.stream(roomIdArray).mapToLong(Long::parseLong).toArray();
                if (roomIds.length >= 2) {
                    String propertyName = housePropertyMapper.selectHousePropertyByPropertyId(roomIds[0]).getPropertyName();
                    String roomNumber = houseRoomInformationMapper.selectHouseRoomInformationByRoomId(roomIds[1]).getRoomNumber();
                    tenantInformation.setRoomId(propertyName + "/" + roomNumber);
                }

            }

        }
        return houseTenantInformations;
    }

    @Override
    public List<HouseTenantInformationVo> selectHouseExpiredTenantInformationList() {
        List<HouseTenantInformationVo> tenantInformations = new ArrayList<>();
        try {
            // 查询所有过期合同
//            List<HouseContract> houseContracts = contractMapper.selectHouseExpiredContractList();
            List<HouseContract> houseContracts=new ArrayList<>();
            if (houseContracts != null) {
                for (HouseContract houseContract : houseContracts) {
                    HouseTenantInformationVo tenantInformationVo=new HouseTenantInformationVo();
                    //开始时间
                    tenantInformationVo.setStartDate(houseContract.getStartDate());
                    //结束时间
                    tenantInformationVo.setEndDate(houseContract.getEndDate());

                    // 确保tenantId不为null
                    if (houseContract.getTenantId() != null) {
                        //根据租户ID查询租户详细信息
                        HouseTenantInformation houseTenantInformation = houseTenantInformationMapper.selectHouseTenantInformationByTenantId(houseContract.getTenantId());
                        if (houseTenantInformation != null) {
                           //租户姓名、手机号、身份证号
                            tenantInformationVo.setTenantName(houseTenantInformation.getTenantName());
                            tenantInformationVo.setTenantPhone(houseTenantInformation.getTenantPhone());
                            tenantInformationVo.setTenantIdNumber(houseTenantInformation.getTenantIdNumber());
                            tenantInformationVo.setRoomNumber(houseRoomInformationMapper.selectHouseRoomInformationByRoomId(Long.valueOf(houseTenantInformation.getRoomId())).getRoomNumber());
                        }

                    }
                    tenantInformations.add(tenantInformationVo);
                }
            }
        } catch (Exception e) {
            // 这里可以记录日志或者进行其他异常处理
            e.printStackTrace();
        }
        return tenantInformations;
    }


    @Override
    public List<Object> selectHouseTenantInformationTree() {
        HouseProperty houseProperty = new HouseProperty();
        List<HouseProperty> houseProperties = housePropertyMapper.selectHousePropertyList(houseProperty);
        HouseRoomInformation houseRoomInformation = new HouseRoomInformation();
        List<HouseRoomInformation> houseRoomInformations = houseRoomInformationMapper.selectHouseRoomInformationList(houseRoomInformation);

        // 创建一个用于存储最终结果的对象列表
        List<Object> result = new ArrayList<>();

        // 创建一个映射，用于通过propertyId关联HouseRoomInformation
        Map<Long, List<HouseRoomInformation>> roomInfoMap = new HashMap<>();
        for (HouseRoomInformation info : houseRoomInformations) {
            roomInfoMap.computeIfAbsent(info.getPropertyId(), k -> new ArrayList<>()).add(info);
        }

        // 遍历房产信息，构建树形结构
        for (HouseProperty property : houseProperties) {
            Map<String, Object> propertyNode = new HashMap<>();
            propertyNode.put("value", property.getPropertyId());
            propertyNode.put("label", property.getPropertyName());
            propertyNode.put("children", roomInfoMap.getOrDefault(property.getPropertyId(), new ArrayList<>()));

            // 将HouseRoomInformation的数据转换为子节点
            List<Map<String, Object>> children = new ArrayList<>();
            for (HouseRoomInformation roomInfo : roomInfoMap.getOrDefault(property.getPropertyId(), new ArrayList<>())) {
                Map<String, Object> roomNode = new HashMap<>();
                roomNode.put("value", roomInfo.getRoomId());
                roomNode.put("label", roomInfo.getRoomNumber());
                children.add(roomNode);
            }
            propertyNode.put("children", children);

            // 添加到结果列表中
            result.add(propertyNode);
        }

        return result;
    }

    @Override
    public List<Object> selectHouseTenantInformationTreeText() {
        HouseProperty houseProperty = new HouseProperty();
        List<HouseProperty> houseProperties = housePropertyMapper.selectHousePropertyList(houseProperty);
        HouseRoomInformation houseRoomInformation = new HouseRoomInformation();
        List<HouseRoomInformation> houseRoomInformations = houseRoomInformationMapper.selectHouseRoomInformationList(houseRoomInformation);

        // 创建一个用于存储最终结果的对象列表
        List<Object> result = new ArrayList<>();

        // 创建一个映射，用于通过propertyId关联HouseRoomInformation
        Map<Long, List<HouseRoomInformation>> roomInfoMap = new HashMap<>();
        for (HouseRoomInformation info : houseRoomInformations) {
            roomInfoMap.computeIfAbsent(info.getPropertyId(), k -> new ArrayList<>()).add(info);
        }

        // 遍历房产信息，构建树形结构
        for (HouseProperty property : houseProperties) {
            Map<String, Object> propertyNode = new HashMap<>();
            propertyNode.put("value", "A"+property.getPropertyId());
            propertyNode.put("text", property.getPropertyName());
            propertyNode.put("children", roomInfoMap.getOrDefault(property.getPropertyId(), new ArrayList<>()));

            // 将HouseRoomInformation的数据转换为子节点
            List<Map<String, Object>> children = new ArrayList<>();
            for (HouseRoomInformation roomInfo : roomInfoMap.getOrDefault(property.getPropertyId(), new ArrayList<>())) {
                Map<String, Object> roomNode = new HashMap<>();
                roomNode.put("value", roomInfo.getRoomId());
                roomNode.put("text", roomInfo.getRoomNumber());
                children.add(roomNode);
            }
            propertyNode.put("children", children);

            // 添加到结果列表中
            result.add(propertyNode);
        }

        return result;
    }

    /**
     * 新增存储租户的基本信息及与房间的关联
     *
     * @param houseTenantInformation 存储租户的基本信息及与房间的关联
     * @return 结果
     */
    @Override
    public int insertHouseTenantInformation(HouseTenantInformation houseTenantInformation) {
        return houseTenantInformationMapper.insertHouseTenantInformation(houseTenantInformation);
    }

    /**
     * 修改存储租户的基本信息及与房间的关联
     *
     * @param houseTenantInformation 存储租户的基本信息及与房间的关联
     * @return 结果
     */
    @Override
    public int updateHouseTenantInformation(HouseTenantInformation houseTenantInformation) {
        return houseTenantInformationMapper.updateHouseTenantInformation(houseTenantInformation);
    }

    /**
     * 批量删除存储租户的基本信息及与房间的关联
     *
     * @param tenantIds 需要删除的存储租户的基本信息及与房间的关联主键
     * @return 结果
     */
    @Override
    public int deleteHouseTenantInformationByTenantIds(Long[] tenantIds) {
        return houseTenantInformationMapper.deleteHouseTenantInformationByTenantIds(tenantIds);
    }

    /**
     * 删除存储租户的基本信息及与房间的关联信息
     *
     * @param tenantId 存储租户的基本信息及与房间的关联主键
     * @return 结果
     */
    @Override
    public int deleteHouseTenantInformationByTenantId(Long tenantId) {
        return houseTenantInformationMapper.deleteHouseTenantInformationByTenantId(tenantId);
    }
}
